/*
 * Copyright 2011 John Alström
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package se.elva.slticket;

import se.elva.slticket.dialog.InfoDialog;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class LanstrafikenSormland extends SmsTicket {

	private int ticketPrice = 0;

	private final String FULLPRICE = "V";
	private final String TEEN = "S";
	private String city = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sormlandlayout);
		updateTicketPrice();
		((RadioButton) findViewById(R.id.RadioButtonSormlandVuxen))
				.setOnCheckedChangeListener(priceChangeListener);
		((RadioButton) findViewById(R.id.RadioButtonSormlandUngdom))
				.setOnCheckedChangeListener(priceChangeListener);

		((Button) findViewById(R.id.send)).setOnClickListener(mBackListener);
		((Button) findViewById(R.id.cancel))
				.setOnClickListener(mCancelListener);

		setPickCitySpinner();
		((Spinner) findViewById(R.id.pickCity))
				.setOnItemSelectedListener(mListener);
		city = "E";
	}

	OnItemSelectedListener mListener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int which,
				long arg3) {
			if (which == 0) {
				city = "E";
			} else if (which == 1) {
				city = "Kh";
			} else if (which == 2) {
				city = "N";
			} else if (which == 3) {
				city = "Sn";
			}
			updateTicketPrice();

		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub

		}
	};

	private void setPickCitySpinner() {
		Spinner s = (Spinner) findViewById(R.id.pickCity);
		ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,
				R.array.citySormland, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);

	}

	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
	// = listeners
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

	OnCheckedChangeListener priceChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
			updateTicketPrice();
		}
	};

	OnClickListener mPickCityListener = new OnClickListener() {
		public void onClick(View v) {
			showDialog(1);
		}
	};

	OnClickListener mBackListener = new OnClickListener() {
		public void onClick(View v) {
			String price = getPrice();
			// "72372"
			if (!"".equals(city) && !"".equals(price)) {
				sendSms(getPhoneNumer(), city + price,
						LanstrafikenSormland.this);
			} else {
				showInfoDialog("Fel", getString(R.string.errorZone));
			}
		}
	};

	private void updateTicketPrice() {
		String priceCategory = getPrice();

		if (FULLPRICE.equalsIgnoreCase(priceCategory)) {
			ticketPrice = 22;
		} else {
			ticketPrice = 11;
		}
		((TextView) findViewById(R.id.Summa)).setText(getString(
				R.string.ticketPrice).replace(":ticketPrice",
				String.valueOf(ticketPrice)));
		setCodeToSend(priceCategory);
	}

	private void setCodeToSend(String priceCategory) {
		TextView codeView = (TextView) findViewById(R.id.code);
		String value = getString(R.string.smsInformationGenrell);
		value = value.replace(":smsCode", city + priceCategory);
		value = value.replace(":telefon", getPhoneNumer());
		codeView.setText(value);
	}

	private String getPrice() {
		if (((RadioButton) findViewById(R.id.RadioButtonSormlandVuxen))
				.isChecked()) {
			return FULLPRICE;
		} else if (((RadioButton) findViewById(R.id.RadioButtonSormlandUngdom))
				.isChecked()) {
			return TEEN;
		}
		return "";
	}

	@Override
	protected void showInfoDialog(String title, String msg) {
		final InfoDialog info = new InfoDialog(LanstrafikenSormland.this, msg);
		info.show();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if (id == 0) {
			return new AlertDialog.Builder(LanstrafikenSormland.this)
					.setTitle(R.string.pickCity)
					.setItems(R.array.city,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									saveChoice = which;
									Intent intent = findIntentToStart(
											LanstrafikenSormland.this, which);
									startActivity(intent);
									finish();
								}
							}).create();
		} else {
			return new AlertDialog.Builder(LanstrafikenSormland.this)
					.setTitle(R.string.pickCity)
					.setItems(R.array.citySormland,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									if (which == 0) {
										((Button) findViewById(R.id.pickCity))
												.setText("Eskilstuna");
										city = "E";
									} else if (which == 1) {
										((Button) findViewById(R.id.pickCity))
												.setText("Katrineholm");
										city = "Kh";
									} else if (which == 2) {
										((Button) findViewById(R.id.pickCity))
												.setText("Nyköping");
										city = "N";
									} else if (which == 3) {
										((Button) findViewById(R.id.pickCity))
												.setText("Strängnäs");
										city = "Sn";
									}
								}
							}).create();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		SharedPreferences prefs = getSharedPreferences("SLTicket", 0);
		String restoredText = prefs.getString("saveChoice", null);
		if (restoredText != null) {
			saveChoice = Integer.valueOf(restoredText);
		} else {
			showDialog(0);
		}
	}

	@Override
	public String getPhoneNumer() {
		return getPrefix() + Constans.PHONENUMBER_SORMLAND;
	}

	@Override
	public String getTextToSend() {
		return city + getPrice();
	}

}
